package com.lbx.controller;

import com.lbx.entity.User;
import com.lbx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吕兵星
 * @date 2020/10/24 15:46
 */
@Controller
public class TestController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping(value = "/t1",produces = "text/html;charset=utf-8")
    public String test1(String name){
        return name!=null&&!"".equals(name.trim())?"状态:true |"+name:"false";
    }


    @ResponseBody
    @GetMapping("/t2")
    public List<User> test2(){
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(new User("吕冰星"+i,"123"+i));
        }
        return list;
    }


    @GetMapping("/toLogin")
    public String login(){
        return "login" ;
    }

    @ResponseBody
    @PostMapping("/checkName")
    public Boolean checkName(String uname){
        if (uname!=null&&!"".equals(uname)){
            return true ;
        }else {
            return false;
        }

    }
    @ResponseBody
    @PostMapping("/checkPassword")
    public Boolean checkPassword(String pwd){
        int pwdLength=6;
        if (pwd!=null&&!"".equals(pwd)&&pwd.trim().length()>pwdLength){
            return true ;
        }else {
            return false;
        }

    }
    @ResponseBody
    @PostMapping("/checkLogin")
    public Boolean checkLogin(String n, String p, HttpServletRequest request){
        if (userService.checkLogin(n,p)) {
            HttpSession session = request.getSession();
            session.setAttribute("uname",n);
            return true;
        }else {
            return false;

        }

    }
    @GetMapping("/toSuccess")
    public String toSuccess(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.getAttribute("uname");
        return "home";
    }
}
